package b4.model
{
	import b4.ApplicationFacade;
	import b4.config.Config;
	import b4.model.vo.resource.ResourceVO;
	import b4.util.GameProfile;
	import b4.util.Resource;
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.system.ApplicationDomain;
	
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.proxy.Proxy;
    
	public class ResourceLoader extends Proxy implements IProxy
	{
		public static const TYPE_MAIN:String 	= "MainResource";
		public static const TYPE_TOWN:String	= "TownResource";
		private var type:String;
		
		public function ResourceLoader(name:String, type:String)
		{
			super(name, new ResourceVO());
			this.type = type;
		}
		
		public function loadResources(urlNames:Array):void
		{
			if (urlNames != null && urlNames.length > 0)
			{
				this.vo.urlNames = urlNames;
				this.vo.appDomains = new Object();
				this.vo.dataXML = new Object();
				loadResourceByIndex(0);
			} 
		}
		
		private function loadResourceByIndex(index:int):void
		{
			this.vo.currentIndex = index;
			var urlName:String = this.vo.urlNames[index];
			var resourceUrl:String;
			if (urlName.split(".")[1].toLowerCase() == "swf")
			{
				resourceUrl = GameProfile.URL_ResourceRoot + urlName;
				var loader:Loader = new Loader();
				loader.load(new URLRequest(resourceUrl));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onResourceLoaded);
				loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onResourceProgress);
			}
			else
			{
				resourceUrl = GameProfile.URL_ConfigRoot + urlName;
				//加载游戏参数设定文件
				var urlLoader:URLLoader = new URLLoader();
				urlLoader.load(new URLRequest(resourceUrl));
				urlLoader.addEventListener(Event.COMPLETE, onResourceLoaded);
				urlLoader.addEventListener(ProgressEvent.PROGRESS, onResourceProgress);
			}
		}
		
		private function onResourceLoaded(event:Event):void
		{
			var urlName:String = this.vo.urlNames[this.vo.currentIndex];
			if (urlName.split(".")[1].toLowerCase() == "swf")
			{
				var appDomain:ApplicationDomain = event.target.applicationDomain;
				this.vo.appDomains[urlName] = appDomain;
			}
			else
			{
				var dataXML:XML = XML(event.target.data);
				this.vo.dataXML[urlName] = dataXML;
			}
			this.vo.currentIndex++;
			if (this.vo.currentIndex < this.vo.urlNames.length)
			{
				loadResourceByIndex(this.vo.currentIndex);
			}
			else
			{
		    	for (var domainName:String in this.vo.appDomains)
		    	{
		    		Resource.appDomains[domainName] = this.vo.appDomains[domainName];
		    	}
		    	for (var xmlName:String in this.vo.dataXML)
		    	{
		    		Config.parseXML(this.vo.dataXML[xmlName]);
		    	}
				sendNotification(ApplicationFacade.RESOURCE_LOADED, null, type);
				facade.removeProxy(getProxyName());
			}
		}
		
		private function onResourceProgress(event:ProgressEvent):void
		{
			this.vo.currentProgress = event.bytesLoaded / event.bytesTotal;
			sendNotification(ApplicationFacade.RESOURCE_PROGRESS, null, type);
		}
		
		public function get vo():ResourceVO
		{
			return data as ResourceVO;
		}
	}
}